//
//  UIButton+AGAddtion.m
//  Gunterapple
//
//  Created by 热带雨林 on 2020/4/3.
//  Copyright © 2020 热带雨林. All rights reserved.
//

#import "UIButton+AGAddtion.h"

//#import <AppKit/AppKit.h>


@implementation UIButton (AGAddtion)

+ (instancetype)initButtonWithFrame:(CGRect)frame withImage:(UIImage *)image withTitle:(NSString *)title withTitleColor:(UIColor *)color withFont:(UIFont *)font withSpace:(CGFloat)space withType:(ButtonImageAndTitleType)type withTarget:(id)target withAction:(SEL)action{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = frame;
    [btn setUpButtonWithImage:image withTitle:title withTitleColor:color withFont:font withSpace:space withType:type];
    [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    return btn;
}
- (void)setUpButtonWithImage:(UIImage *)image withTitle:(NSString *)title withTitleColor:(UIColor *)color withFont:(UIFont *)font withSpace:(CGFloat)space withType:(ButtonImageAndTitleType)type{
    [self setImage:image forState:UIControlStateNormal];
    [self setTitle:title forState:UIControlStateNormal];
    [self setTitleColor:color forState:UIControlStateNormal];
    self.titleLabel.font = font;
    //获取按钮 文字 图片的size
    CGSize btnSize = self.frame.size;
    CGSize titleSize = self.titleLabel.intrinsicContentSize;
    CGSize imgSize = self.imageView.frame.size;
    //根据图片的位置进行判断
    switch (type) {
        case GD_Top:{
            CGFloat verticalSpace = (btnSize.height - titleSize.height - imgSize.height - space) / 2.0;
            
            [self setImageEdgeInsets:UIEdgeInsetsMake(verticalSpace, (btnSize.width - imgSize.width) / 2.0, verticalSpace + titleSize.height + space, (btnSize.width - imgSize.width) / 2.0  - titleSize.width)];
            [self setTitleEdgeInsets:UIEdgeInsetsMake(verticalSpace + imgSize.height + space, (btnSize.width - titleSize.width) / 2.0 - imgSize.width, verticalSpace, (btnSize.width - titleSize.width) / 2.0)];
            break;
        }
        case GD_Left:{
            [self setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, space)];
            [self setTitleEdgeInsets:UIEdgeInsetsMake(0, space, 0, 0)];
            break;
        }
        case GD_Right:{
            [self setImageEdgeInsets:UIEdgeInsetsMake(0, btnSize.width - imgSize.width - titleSize.width - space, 0, -(imgSize.width + titleSize.width + space))];
            [self setTitleEdgeInsets:UIEdgeInsetsMake(0, -(imgSize.width + titleSize.width + space), 0, -(btnSize.width - imgSize.width - titleSize.width - space))];
            break;
        }
        case GD_Bottom:{
            CGFloat verticalSpace = (btnSize.height - titleSize.height - imgSize.height - space) / 2.0;
            [self setImageEdgeInsets:UIEdgeInsetsMake(verticalSpace + titleSize.height + space, (btnSize.width - titleSize.width) / 2.0 - imgSize.width, verticalSpace , (btnSize.width - imgSize.width) / 2.0 - titleSize.width)];
            [self setTitleEdgeInsets:UIEdgeInsetsMake(verticalSpace, (btnSize.width - titleSize.width) / 2.0 - imgSize.width, verticalSpace + imgSize.height + space, (btnSize.width - titleSize.width) / 2.0)];
            break;
        }
        default:
            break;
    }
    
}
@end
